﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using A2DFramework.HttpLoadBalancer.HttpProtocal;

namespace A2DFramework.HttpLoadBalancer.Parsers.ConcreteParsers
{
    class RequestLineParser : IRequestLineParser
    {
        public HttpProtocal.RequestLine Parse(string rawHttp_FirstRow)
        {
            //GET /file.aspx HTTP/1.1
            int spaceIndex1 = rawHttp_FirstRow.IndexOf(" ");
            int spaceIndex2 = rawHttp_FirstRow.IndexOf(" ", spaceIndex1 + 1);

            string actionCode = rawHttp_FirstRow.Substring(0, spaceIndex1).Trim();
            string pathCode = rawHttp_FirstRow.Substring(spaceIndex1, spaceIndex2 - spaceIndex1).Trim();
            string versionCode = rawHttp_FirstRow.Substring(spaceIndex2).Trim();


            HttpRequestActions action = Helper.ParseHttpAction(actionCode);

            HttpProtocalVersions version = Helper.ParseHttpVersion(versionCode);

            return new HttpProtocal.RequestLine()
            {
                Action = action,
                Path = pathCode,
                Version = version
            };
        }
    }
}
